{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "35863c79",
   "metadata": {},
   "source": [
    "1.使用PCA进行降维"
   ]
  },
  {
   "cell_type": "raw",
   "id": "865f12b6",
   "metadata": {},
   "source": [
    "1.去中心化\n",
    "2.生成协方差矩阵\n",
    "3.求协方差的特征值和特征向量\n",
    "4.特征向量与数据相乘"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "518ccb24",
   "metadata": {},
   "source": [
    "1.读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "44f98ce5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(15120, 56)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('./train_forest_covertype.csv')\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4c7541ae",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 15120 entries, 0 to 15119\n",
      "Data columns (total 56 columns):\n",
      " #   Column                              Non-Null Count  Dtype\n",
      "---  ------                              --------------  -----\n",
      " 0   Id                                  15120 non-null  int64\n",
      " 1   Elevation                           15120 non-null  int64\n",
      " 2   Aspect                              15120 non-null  int64\n",
      " 3   Slope                               15120 non-null  int64\n",
      " 4   Horizontal_Distance_To_Hydrology    15120 non-null  int64\n",
      " 5   Vertical_Distance_To_Hydrology      15120 non-null  int64\n",
      " 6   Horizontal_Distance_To_Roadways     15120 non-null  int64\n",
      " 7   Hillshade_9am                       15120 non-null  int64\n",
      " 8   Hillshade_Noon                      15120 non-null  int64\n",
      " 9   Hillshade_3pm                       15120 non-null  int64\n",
      " 10  Horizontal_Distance_To_Fire_Points  15120 non-null  int64\n",
      " 11  Wilderness_Area1                    15120 non-null  int64\n",
      " 12  Wilderness_Area2                    15120 non-null  int64\n",
      " 13  Wilderness_Area3                    15120 non-null  int64\n",
      " 14  Wilderness_Area4                    15120 non-null  int64\n",
      " 15  Soil_Type1                          15120 non-null  int64\n",
      " 16  Soil_Type2                          15120 non-null  int64\n",
      " 17  Soil_Type3                          15120 non-null  int64\n",
      " 18  Soil_Type4                          15120 non-null  int64\n",
      " 19  Soil_Type5                          15120 non-null  int64\n",
      " 20  Soil_Type6                          15120 non-null  int64\n",
      " 21  Soil_Type7                          15120 non-null  int64\n",
      " 22  Soil_Type8                          15120 non-null  int64\n",
      " 23  Soil_Type9                          15120 non-null  int64\n",
      " 24  Soil_Type10                         15120 non-null  int64\n",
      " 25  Soil_Type11                         15120 non-null  int64\n",
      " 26  Soil_Type12                         15120 non-null  int64\n",
      " 27  Soil_Type13                         15120 non-null  int64\n",
      " 28  Soil_Type14                         15120 non-null  int64\n",
      " 29  Soil_Type15                         15120 non-null  int64\n",
      " 30  Soil_Type16                         15120 non-null  int64\n",
      " 31  Soil_Type17                         15120 non-null  int64\n",
      " 32  Soil_Type18                         15120 non-null  int64\n",
      " 33  Soil_Type19                         15120 non-null  int64\n",
      " 34  Soil_Type20                         15120 non-null  int64\n",
      " 35  Soil_Type21                         15120 non-null  int64\n",
      " 36  Soil_Type22                         15120 non-null  int64\n",
      " 37  Soil_Type23                         15120 non-null  int64\n",
      " 38  Soil_Type24                         15120 non-null  int64\n",
      " 39  Soil_Type25                         15120 non-null  int64\n",
      " 40  Soil_Type26                         15120 non-null  int64\n",
      " 41  Soil_Type27                         15120 non-null  int64\n",
      " 42  Soil_Type28                         15120 non-null  int64\n",
      " 43  Soil_Type29                         15120 non-null  int64\n",
      " 44  Soil_Type30                         15120 non-null  int64\n",
      " 45  Soil_Type31                         15120 non-null  int64\n",
      " 46  Soil_Type32                         15120 non-null  int64\n",
      " 47  Soil_Type33                         15120 non-null  int64\n",
      " 48  Soil_Type34                         15120 non-null  int64\n",
      " 49  Soil_Type35                         15120 non-null  int64\n",
      " 50  Soil_Type36                         15120 non-null  int64\n",
      " 51  Soil_Type37                         15120 non-null  int64\n",
      " 52  Soil_Type38                         15120 non-null  int64\n",
      " 53  Soil_Type39                         15120 non-null  int64\n",
      " 54  Soil_Type40                         15120 non-null  int64\n",
      " 55  Cover_Type                          15120 non-null  int64\n",
      "dtypes: int64(56)\n",
      "memory usage: 6.5 MB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b76fa6b2",
   "metadata": {},
   "source": [
    "2.PCA降维"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "835c831e",
   "metadata": {},
   "source": [
    "使用PCA方法对数据进行降维，要求保留原始的信息解释比不低于96%。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "348900a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "\n",
    "# 定义规则\n",
    "model_pca = PCA(n_components=0.96)  # n_components为整数时就表示将数降维到多少列。如果为小数就表示降维时保留数据的信息比\n",
    "# 其取值范围在(0-1)之间，不能取0或者1\n",
    "# 将规则应用到数上\n",
    "df_pca1 = model_pca.fit_transform(df)   \n",
    "# 转型  上面降维之后等于是生成了一份全新的数据，和原来的df关系就不大了，那么列名肯定和df也不一样了\n",
    "df_pca1 = pd.DataFrame(data=df_pca1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "50d5cf7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>7634.498455</td>\n",
       "      <td>1512.003064</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7631.437404</td>\n",
       "      <td>1382.064518</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7656.355884</td>\n",
       "      <td>3631.443319</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7656.423862</td>\n",
       "      <td>3603.934258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7627.653363</td>\n",
       "      <td>1352.499714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15115</th>\n",
       "      <td>-7566.502710</td>\n",
       "      <td>-883.126774</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15116</th>\n",
       "      <td>-7568.922607</td>\n",
       "      <td>-882.551544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15117</th>\n",
       "      <td>-7572.468413</td>\n",
       "      <td>-1217.530224</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15118</th>\n",
       "      <td>-7578.543195</td>\n",
       "      <td>-1439.766976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15119</th>\n",
       "      <td>-7579.847544</td>\n",
       "      <td>-1425.558549</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>15120 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                 0            1\n",
       "0      7634.498455  1512.003064\n",
       "1      7631.437404  1382.064518\n",
       "2      7656.355884  3631.443319\n",
       "3      7656.423862  3603.934258\n",
       "4      7627.653363  1352.499714\n",
       "...            ...          ...\n",
       "15115 -7566.502710  -883.126774\n",
       "15116 -7568.922607  -882.551544\n",
       "15117 -7572.468413 -1217.530224\n",
       "15118 -7578.543195 -1439.766976\n",
       "15119 -7579.847544 -1425.558549\n",
       "\n",
       "[15120 rows x 2 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pca1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "280c94cf",
   "metadata": {},
   "source": [
    "使用PCA算法对数据df进行降维要求降维到20列。降维时不对目标列进行降维。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "06761a5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "\n",
    "# 定义规则\n",
    "model_pca = PCA(n_components=20) \n",
    "df_pca2 = model_pca.fit_transform(df.drop(axis=1,columns='Cover_Type'))\n",
    "# 转型\n",
    "df_pca2 = pd.DataFrame(data=df_pca2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ff137007",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_pca2['Cover_Type'] = df['Cover_Type']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cd68c963",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(15120, 21)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pca2.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96419ac9",
   "metadata": {},
   "source": [
    "3.LDA降维"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bfa65a7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA\n",
    "X = df.drop(axis=1,columns='Cover_Type')\n",
    "Y = df['Cover_Type']\n",
    "# 定义规则\n",
    "model_lda = LDA(n_components=6)\n",
    "# 将规则应用到数上\n",
    "df_lda1 = model_lda.fit_transform(X,Y)\n",
    "# 转型\n",
    "df_lda1 = pd.DataFrame(data=df_lda1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0a0751b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.560756</td>\n",
       "      <td>2.005447</td>\n",
       "      <td>0.723488</td>\n",
       "      <td>1.158643</td>\n",
       "      <td>-2.497388</td>\n",
       "      <td>-0.170733</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.567847</td>\n",
       "      <td>1.971743</td>\n",
       "      <td>0.730784</td>\n",
       "      <td>1.218850</td>\n",
       "      <td>-2.440230</td>\n",
       "      <td>-0.085597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.931962</td>\n",
       "      <td>1.928733</td>\n",
       "      <td>1.256382</td>\n",
       "      <td>-0.577142</td>\n",
       "      <td>-5.814389</td>\n",
       "      <td>1.184942</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.874259</td>\n",
       "      <td>1.806457</td>\n",
       "      <td>0.538736</td>\n",
       "      <td>3.037597</td>\n",
       "      <td>-1.193435</td>\n",
       "      <td>0.302722</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.593692</td>\n",
       "      <td>1.925574</td>\n",
       "      <td>0.713169</td>\n",
       "      <td>1.260206</td>\n",
       "      <td>-2.330022</td>\n",
       "      <td>-0.131985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15115</th>\n",
       "      <td>-2.097453</td>\n",
       "      <td>0.570581</td>\n",
       "      <td>-1.887371</td>\n",
       "      <td>-0.474777</td>\n",
       "      <td>0.640116</td>\n",
       "      <td>2.891088</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15116</th>\n",
       "      <td>-2.633722</td>\n",
       "      <td>0.507592</td>\n",
       "      <td>-1.322361</td>\n",
       "      <td>0.658729</td>\n",
       "      <td>0.578820</td>\n",
       "      <td>3.229163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15117</th>\n",
       "      <td>-3.097633</td>\n",
       "      <td>0.458531</td>\n",
       "      <td>-1.378195</td>\n",
       "      <td>0.947894</td>\n",
       "      <td>0.551642</td>\n",
       "      <td>3.095824</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15118</th>\n",
       "      <td>-3.003674</td>\n",
       "      <td>0.446191</td>\n",
       "      <td>-1.553627</td>\n",
       "      <td>0.943117</td>\n",
       "      <td>0.633923</td>\n",
       "      <td>3.618854</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15119</th>\n",
       "      <td>-2.666482</td>\n",
       "      <td>0.853275</td>\n",
       "      <td>-2.786993</td>\n",
       "      <td>0.215945</td>\n",
       "      <td>0.253136</td>\n",
       "      <td>2.958611</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>15120 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              0         1         2         3         4         5\n",
       "0      0.560756  2.005447  0.723488  1.158643 -2.497388 -0.170733\n",
       "1      0.567847  1.971743  0.730784  1.218850 -2.440230 -0.085597\n",
       "2      1.931962  1.928733  1.256382 -0.577142 -5.814389  1.184942\n",
       "3      0.874259  1.806457  0.538736  3.037597 -1.193435  0.302722\n",
       "4      0.593692  1.925574  0.713169  1.260206 -2.330022 -0.131985\n",
       "...         ...       ...       ...       ...       ...       ...\n",
       "15115 -2.097453  0.570581 -1.887371 -0.474777  0.640116  2.891088\n",
       "15116 -2.633722  0.507592 -1.322361  0.658729  0.578820  3.229163\n",
       "15117 -3.097633  0.458531 -1.378195  0.947894  0.551642  3.095824\n",
       "15118 -3.003674  0.446191 -1.553627  0.943117  0.633923  3.618854\n",
       "15119 -2.666482  0.853275 -2.786993  0.215945  0.253136  2.958611\n",
       "\n",
       "[15120 rows x 6 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_lda1 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eea8637a",
   "metadata": {},
   "source": [
    "4.LDA分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9a7706dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 2, ..., 3, 3, 3], dtype=int64)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA\n",
    "\n",
    "# 建模\n",
    "model = LDA()\n",
    "model.fit(X,Y)\n",
    "# 预测\n",
    "model.predict(X)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
